/**
 * Introduction HTTP 同步请求的类
 *
 * @author 刘维中
 * @email weizhong.lulu@gmail.com
 * @since 1.0
 * @date 2021/05/31
 */
import axios from 'axios'
import { Message } from 'element-ui';

// HTTP 请求对象
const TIME_OUT = 150000;
const http = axios.create ( {
    timeout: TIME_OUT,
    baseURL:"http://localhost:3000"
} )


// HTTP 接口请求配置
http.interceptors.request.use (
    ( config ) =>
    {
        // 请求的Token的值
        // const accessToken           = window.localStorage.getItem('current-access-token')
        return config
    },
    ( error ) =>
    {
        return Promise.reject ( error )
    }
)

// HTTP 接口响应
http.interceptors.response.use (
    ( response ) =>
    {
        return Promise.resolve ( response )
    },
    ( error ) =>
    {
        Message.error('失败：' + error)
        return Promise.reject ( error )
    }
)



// 返回 POST 同步请求
export function PostHttp ( url, payload )
{
    return http.post ( url, payload )
}

// 返回 GET 同步请求
export function GetHttp ( url )
{
    return http.get ( url )
}
// 返回 PUT 同步请求
export function PutHttp ( url,payload )
{
    return http.put ( url, payload )
}


// 超时1000的请求
const httpShort = axios.create ( {
    timeout: 1000,
    baseURL:process.env.VUE_APP_URL
} )

// HTTP 接口请求配置
httpShort.interceptors.request.use (
    ( config ) =>
    {
        return config
    },
    ( error ) =>
    {
        return Promise.reject ( error )
    }
)

// HTTP 接口响应
httpShort.interceptors.response.use (
    ( response ) =>
    {
        return Promise.resolve ( response )
    },
    ( error ) =>
    {
        return Promise.reject ( error )
    }
)

// 返回 GET 同步请求
export function GetHttpShort ( url )
{
    return httpShort.get ( url )
}

// 返回 HTTP 请求服务 - 用于异常请求
export default http
